package com.hskn.hss.module.tlchannel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlchannel.entity.TlChannel;
import com.hskn.hss.module.tlchannel.mapper.TlChannelMapper;
import com.hskn.hss.module.tlchannel.service.ITlChannelService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-11
 */
@Service
public class TlChannelServiceImpl extends ServiceImpl<TlChannelMapper, TlChannel> implements ITlChannelService {

    @Resource
    TlChannelMapper tlChannelMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlChannel tlChannel = params.getObject("data", TlChannel.class);

        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlChannel.setCreateby(uid);
        tlChannel.setCreatetime(new Date());
        tlChannel.setDeptid(deptid);
        tlChannel.setStatus("1");
        int i = tlChannelMapper.insert(tlChannel);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlChannel tlChannel = params.getObject("data", TlChannel.class);
        tlChannel.setUpdateby(uid);
        tlChannel.setUpdatetime(new Date());
        int i = tlChannelMapper.updateById(tlChannel);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlChannelMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public TlChannel getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlChannel tlChannel = tlChannelMapper.selectById(id);
        return tlChannel;
    }


    @Override
    public List getList(String equipid) throws HssException {

        JSONObject json = new JSONObject();
        Map<String, Object> map = new HashMap();
        map.put("equipid", equipid);
//        Page<TlChannel> listPage = new Page<>(page, size, true);
//        IPage<TlChannel> iPage = tlChannelMapper.listchannel(listPage, map);
//        json.put("total", iPage.getTotal());
//        json.put("size", iPage.getSize());
//        json.put("pages", iPage.getPages());
//        json.put("current", iPage.getCurrent());
//        json.put("equiplist", iPage.getRecords());

        List<TlChannel> resultList = tlChannelMapper.listchannel(map);
        json.put("resultList", resultList);
        return resultList;
    }
}
